# waveplugs/wave.py
#
#

""" wave related commands. """

## gozerlib imports

from gozerlib.commands import cmnds
from gozerlib.examples import examples
from gozerlib.utils.exception import handle_exception

## waveapi imports

from waveapi import document

def handle_waveid(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return
    event.reply(event.waveid)

cmnds.add('wave-id', handle_waveid, 'USER')
examples.add('wave-id', 'show the id of the wave the command is given in.', 'wave-id')

def handle_waveurl(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return
    event.reply(event.url)

cmnds.add('wave-url', handle_waveurl, 'USER')
examples.add('wave-url', 'show the url of the wave the command is given in.', 'wave-url')

def handle_waveparticipants(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return
    event.reply("participants: ", list(event.root.GetParticipants()))

cmnds.add('wave-participants', handle_waveparticipants, 'USER')
examples.add('wave-participants', 'show the participants of the wave the command is given in.', 'wave-participants')

def handle_waveattr(bot, event):

    """ debug function to show attributes of wave event. """

    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    if not event.rest:
        event.missing("<attribute> .. one of: %s " % event.keys())
        return

    attr = getattr(event, event.rest)

    if not attr:
        event.reply("no such attribute available")
        return

    attribs = dir(attr)
    event.reply("attributes of %s:" % event.rest, attribs)
    res = {}

    for a in attribs:

        if '__' in str(a):
            continue 

        if 'get' in str(a).lower():
            aa = getattr(attr, a)
       
            try:
                res[a] = aa()
            except:
                pass
        else:
            res[a] = getattr(attr, a)

    event.reply("values of %s: " % event.rest, [str(res), ])

cmnds.add('wave-attr', handle_waveattr, 'OPER')
examples.add('wave-attr', 'show attributes of wave event', 'wave-attr')

def handle_wavepart(bot, event):
    if event.type != "wave":
        event.reply("this command only works in google wave.");
        return

    event.reply('bye')
    event.root.RemoveSelf()

cmnds.add('wave-part', handle_wavepart, 'OPER')
examples.add('wave-part', 'leave the wave', 'wave-part')
